const { beginDir } = require("./apilist")

let tmpdir = 'D:/xiangmu/shujuguanli/tmpdir/'
let inputdirpath = 'D:/xiangmu/shujuguanli/test/'

let tmpjson = {
  "src_file_path": "",
}
let tmpjson1 = {
  "input_file_path": "",
  "max_length": 256
}

let polygoncmd = 'D:/xiangmu/shujuguanli/ImageProperties/ImageProperties.exe '
let imgcmd = 'D:/xiangmu/shujuguanli/ImageProperties/Thumbnail.exe '


async function beginDir(params) {

  let resnames = await getAllFile(inputdirpath)

  // let cmdstr = 'D:/xiangmu/shujuguanli/ImageProperties/ImageProperties.exe '

  resnames.map(item => {
    let cmdstr = polygoncmd
    // let cmdstr = imgcmd

    // let resitem = item.split("\\").pop()

    // console.log(resitem);

    // tmpjson1.input_file_path = '../test/a.tif'
    tmpjson.src_file_path = item

    let tmpjsonrespath = tmpdir + '' + _.random(0, 9999999999) + '.json'

    fs.writeFileSync(tmpjsonrespath, JSON.stringify(tmpjson), 'utf8')

    cmdstr += tmpjsonrespath

    console.log(cmdstr);

    exec(cmdstr, (err, stdout, stderr) => {
      if (err) {
        console.error(err);
        return;
      }
      console.log(stdout);
    });
  })

  // console.log(resnames);

}
async function getAllFile(directoryPath) {

  return new Promise((resolve, reject) => {
    fs.readdir(directoryPath, (err, files) => {
      if (err) {
        console.log('Error reading directory:', err);
        return;
      }

      let res_arr = []

      files.forEach(file => {
        const filePath = path.join(directoryPath, file);
        // 判断文件是文件还是目录
        let stats = fs.statSync(filePath)
        if (stats.isDirectory()) {
          // console.log(`Directory: ${filePath}`);
        } else {
          // console.log(`File: ${filePath}`);
          res_arr.push(filePath)
        }


      });

      resolve(res_arr)

    });

  })

}

exports.beginDir = beginDir